package cn.lili.controller.distribution;

import cn.hutool.core.util.StrUtil;
import cn.lili.cache.auth.annotation.Authentication;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.distribution.entity.dos.Distribution;
import cn.lili.modules.distribution.entity.dto.DistributionApplyDTO;
import cn.lili.modules.distribution.entity.dto.DistributionSearchParams;
import cn.lili.modules.distribution.entity.vos.DistributionVO;
import cn.lili.modules.distribution.service.DistributionService;
import cn.lili.modules.member.entity.vo.NumberOfOwners;
import cn.lili.modules.order.order.entity.dto.OrderFlowQueryDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 分销员管理接口
 *
 * @author pikachu
 * @since 2020-03-14 23:04:56
 */
@RestController
@Api(tags = "分销员管理接口")
@RequestMapping("/distribution")
@RequiredArgsConstructor
public class DistributionController {

    private final DistributionService distributionService;

    @ApiOperation(value = "分页获取-分销员/店主列表")
    @GetMapping
    public ResultMessage<Page<Distribution>> getByPage(DistributionSearchParams distributionSearchParams, PageVO page) {
        return ResultUtil.data(distributionService.distributionPage(distributionSearchParams, page));
    }

    @ApiOperation(value = "分页获取-分销员/店主列表")
    @GetMapping("/distributionPage")
    public ResultMessage<Page<Distribution>> distributionPage(DistributionSearchParams distributionSearchParams, PageVO page) {
        return ResultUtil.data(distributionService.distributionPage1(distributionSearchParams, page));
    }

    @ApiOperation("获取某个店主的直推,间推佣金收益情况")
    @GetMapping("/demo/distribution")
    public ResultMessage<Object> selectDisLowerAndLowerSubComissByParentId(OrderFlowQueryDTO dto) {
        return ResultUtil.data(distributionService.selectDisLowerAndLowerSubComissByParentId(dto.getMemberId(), null, dto.getDistributionName()));
    }

    @ApiOperation(value = "获取当前会员的分销员信息", notes = "可根据分销员信息查询待提现金额以及冻结金额等信息")
    @GetMapping("/current")
    public ResultMessage<Distribution> getDistribution() {
        //检查分销开关
        distributionService.checkDistributionSetting();

        return ResultUtil.data(distributionService.getDistribution());
    }

    @ApiOperation(value = "获取当前会员的分销员信息", notes = "可根据分销员信息查询待提现金额以及冻结金额等信息")
    @GetMapping("/getDistributionInfo")
    public ResultMessage<Distribution> getDistributionInfo() {
        //检查分销开关
        distributionService.checkDistributionSetting();
        return ResultUtil.data(distributionService.getDistribution());
    }


    /**
     * 分润提现
     * @param distributionSearchParams 提现
     * @return 是否成功
     */
    @PostMapping("/withdraw")
    public ResultMessage<Object> withdraw(DistributionSearchParams distributionSearchParams) {
        return distributionService.withdraw(distributionSearchParams);
    }

    /**
     * 提现审核
     * @param id 提现单id
     * @param auditStatus 审核状态： 1通过，2驳回
     * @param remark 审核备注
     * @return 审核结果
     */
    @PostMapping("/auditWithdraw")
    public ResultMessage<Object> auditWithdraw(@RequestParam String id, @RequestParam String auditStatus, String remark) {
        return distributionService.auditWithdraw(id, auditStatus, remark);
    }

    @ApiOperation(value = "绑定分销员")
    @ApiImplicitParam(name = "distributionId", value = "分销员ID", required = true, paramType = "path")
    @GetMapping("/binding/{distributionId}")
    public ResultMessage<Object> bindingDistribution(@PathVariable String distributionId) {
        distributionService.bindingDistribution(distributionId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "申请分销员")
    @PostMapping
    public ResultMessage<Object> applyDistribution(DistributionApplyDTO distributionApplyDTO) {
        return ResultUtil.data(distributionService.applyDistribution(distributionApplyDTO));
    }

    /**
     * 能否申请成为分销员
     * @return true / false
     */
    @GetMapping("/isApplyDistribution")
    public ResultMessage<Object> isApplyDistribution() {
        return ResultUtil.data(distributionService.isApplyDistribution());
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "清退分销商")
    @PutMapping(value = "/retreat/{id}")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分销商id", required = true, paramType = "path", dataType = "String")})
    public ResultMessage<Object> retreat(@PathVariable String id) {
        if (distributionService.retreat(id)) {
            return ResultUtil.success();
        } else {
            throw new ServiceException(ResultCode.DISTRIBUTION_RETREAT_ERROR);
        }

    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "恢复分销商")
    @PutMapping(value = "/resume/{id}")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分销商id", required = true, paramType = "path", dataType = "String")})
    public ResultMessage<Object> resume(@PathVariable String id) {
        if (distributionService.resume(id)) {
            return ResultUtil.success();
        } else {
            throw new ServiceException(ResultCode.DISTRIBUTION_RETREAT_ERROR);
        }

    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "审核分销商")
    @PutMapping(value = "/audit/{id}")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分销商id", required = true, paramType = "path", dataType = "String"), @ApiImplicitParam(name = "status", value = "审核结果，PASS 通过  REFUSE 拒绝", required = true, paramType = "query", dataType = "String")})
    public ResultMessage<Object> audit(@NotNull @PathVariable String id, @NotNull String status) {
        if (distributionService.audit(id, status)) {
            return ResultUtil.success();
        } else {
            throw new ServiceException(ResultCode.DISTRIBUTION_AUDIT_ERROR);
        }

    }

    @ApiOperation(value = "审核分销商")
    @GetMapping(value = "/distributionOrder")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分销商id", required = true, paramType = "path", dataType = "String"), @ApiImplicitParam(name = "status", value = "审核结果，PASS 通过  REFUSE 拒绝", required = true, paramType = "query", dataType = "String")})
    public ResultMessage<Object> distributionOrder(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize) {
        return ResultUtil.data(distributionService.distributionOrder(pageNumber, pageSize));
    }

    @ApiOperation(value = "店长/分销员信息详情")
    @GetMapping(value = "/selectDistributionById")
    public ResultMessage<Distribution> selectDistributionById(@RequestParam("id") String id) {
        return ResultUtil.data(distributionService.selectDistributionById(id));
    }


    /**
     * 获取城主资格校验
     */
    @GetMapping("/qualificationCheck")
    public ResultMessage<Object> qualificationCheck() {
        return ResultUtil.data(distributionService.qualificationCheck());
    }

    @ApiOperation(value = "搜索店主")
    @GetMapping("/getListBy")
    public ResultMessage<List<Distribution>> getListByName (String keyword) {
        return ResultUtil.data(distributionService.getListByName(keyword));
    }

    /**
     * 运营中心 --》 店主列表
     */
    @GetMapping("/shopkeeperPage")
    public ResultMessage<IPage<DistributionVO>> shopkeeperPage(DistributionVO distributionVO) {
        return ResultUtil.data(distributionService.shopkeeperPage(distributionVO));
    }

    /**
     * 店主总数
     *
     * @param type 1今日，2昨日，3本周，4上周，5本月，6上月，7本季度，8上季度，9今年，10去年
     */
    @GetMapping("/numberOfOwners")
    public ResultMessage<NumberOfOwners> numberOfOwners(@RequestParam Integer type) {
        return ResultUtil.data(distributionService.numberOfOwners(type));
    }

    /**
     * 店主营业额
     */
    @GetMapping("/shopOwnersTurnover")
    public ResultMessage<NumberOfOwners> shopOwnersTurnover(@RequestParam Integer type) {
        return ResultUtil.data(distributionService.shopOwnersTurnover(type));
    }


    @PutMapping("/updateDistributionInfo/{id}")
    @PreventDuplicateSubmissions
    @ApiOperation("修改店主信息")
    public ResultMessage<Object> updateDistributionInfo(@PathVariable("id") String id, @RequestBody Distribution distribution) {
        if (StrUtil.isEmpty(id) || !Pattern.matches("\\d+", id)) {
            return ResultUtil.error(ResultCode.PARAMS_ERROR);
        }
        distribution.setId(id);
        return distributionService.updateDistributionInfo(distribution);
    }
}
